local lor = require("lor.index")
local productRouter = lor:Router() -- 生成一个group router对象
local mysql = require("app.util.mysql_pool")

-----------------------------------------------------------------------------
-----------------------------------页面跳转-----------------------------------
-----------------------------------------------------------------------------

-- 添加产品页面
productRouter:get("/add", function(req, res, next)
    local flag,result,sqlstate, err, errno = mysql:query("SELECT id,name FROM base_info WHERE del=1",true)
    local flag_tags,result_tags,sqlstate_tags, err_tags, errno_tags = mysql:query("SELECT `id`,`name` FROM dict_tags",true)
    -- 渲染页面
    res:render("product/info_add", {
        -- result = result,
        title = "添加产品",
        result = result,
        result_tags = result_tags
    })
end)

-- 编辑产品页面
productRouter:get("/edit/:id", function(req, res, next)
    local id = req.params.id
    local flag,result,sqlstate, err, errno = mysql:queryOne("SELECT * FROM base_info WHERE id = "..id,true)
    local flag_add,result_add,sqlstate_add, err_add, errno_add = mysql:query("SELECT a.id,a.name,b.p_id FROM base_info a LEFT JOIN ref_base b ON a.del=1 AND a.id=b.c_id AND b.p_id="..id,true)
    local flag_tags,result_tags,sqlstate_tags, err_tags, errno_tags = mysql:query("SELECT `id`,`name` FROM dict_tags",true)
    -- 渲染页面
    res:render("product/info_edit", {
        result = result,
        result_add = result_add,
        result_tags = result_tags,
        title = "编辑产品"
    })
end)

-- 查询所有产品
productRouter:get("/query", function(req, res, next)
    local flag,result,sqlstate, err, errno = mysql:query("SELECT a.*,b.name as dict_name FROM base_info a,dict_tags b WHERE a.del=1 AND a.tags=b.id",true)
    -- 渲染页面
    res:render("product/info", {
        result = result
    })
end)

-----------------------------------------------------------------------------
-----------------------------------业务处理-----------------------------------
-----------------------------------------------------------------------------

-- 根据产品名称查找产品
productRouter:get("/query/:id", function(req, res, next)
    local flag,result,sqlstate, err, errno = mysql:queryOne("SELECT * FROM base_info WHERE id = "..req.params.id,true)
    res:json({
        success = flag,
        message = "",
        data = result
    })
end)

-- 添加产品
productRouter:post("/add", function(req, res, next)
    -- 从请求体取参数
    local name = req.body.name
    local cpu = req.body.cpu
    local memory = req.body.memory
    local system = req.body.system
    local concurrent = req.body.concurrent
    local network_type = req.body.network_type
    local network_size = req.body.network_size
    local tags = req.body.tags
    local desc = req.body.desc
    local sql = "INSERT INTO base_info(`name`, `tags`, `desc`, `cpu`, `memory`, `concurrent`, `system`, `network_type`, `network_size`) VALUES ('"..name.."', '"..tags.."', '"..desc.."', "..cpu..", "..memory..", "..concurrent..", "..system..", "..network_type..", "..network_size..")"
    local flag,result,sqlstate, err, errno = mysql:query(sql,true)
    local id = result.insert_id
    local ids = req.body.ids
    -- 如果有依赖模块，则需要补充依赖数据到关系表ref_base
    if flag == true and ids ~= nil then
        local sql_ref = "INSERT INTO ref_base(`p_id`, `c_id`) VALUES "
        local sql_ref_values = ""
        for i, v in ipairs(ids) do
            sql_ref_values = sql_ref_values .. "("..id..", "..v.."),"
        end
        sql_ref_values = string.sub(sql_ref_values, 1, -2)
        sql_ref = sql_ref .. sql_ref_values
        mysql:query(sql_ref,true)
    end

    res:json({
        success = flag,
        message = "",
        data = result
    })
end)

-- 删除产品
productRouter:get("/del/:id", function(req, res, next)
    -- 从请求体取参数
    local id = req.params.id
    local sql = "UPDATE base_info SET del = 0 WHERE id="..id
    local flag,result,sqlstate, err, errno = mysql:query(sql,true)
    if flag == true then
        mysql:query("DELETE FROM ref_base WHERE p_id="..id,true)
    end
    res:json({
        success = flag,
        message = "",
        data = result
    })
end)

-- 恢复产品
productRouter:get("/renew/:id", function(req, res, next)
    -- 从请求体取参数
    local id = req.params.id
    local sql = "UPDATE base_info SET del = 1 WHERE id = " ..id
    local flag,result,sqlstate, err, errno = mysql:query(sql,true)

    res:json({
        success = flag,
        message = "",
        data = result
    })
end)

-- 修改产品
productRouter:post("/update", function(req, res, next)
    -- 从请求体取参数
    local id = req.body.id
    if id == nil then
        res:json({
            success = false,
            message = "id是必填参数！"
        })
    else
        local name = req.body.name
        local cpu = req.body.cpu
        local memory = req.body.memory
        local system = req.body.system
        local concurrent = req.body.concurrent
        local network_type = req.body.network_type
        local network_size = req.body.network_size
        local tags = req.body.tags
        local desc = req.body.desc
        local sql = "UPDATE base_info SET "
        if name ~= nil then
            sql = sql .. ",name = '"..name .. "'"
        end
        if cpu ~= nil then
            sql = sql .. ",cpu = "..cpu
        end
        if memory ~= nil then
            sql = sql .. ",memory = "..memory
        end
        if system ~= nil then
            sql = sql .. ",system = "..system
        end
        if concurrent ~= nil then
            sql = sql .. ",concurrent = "..concurrent
        end
        if network_type ~= nil then
            sql = sql .. ",network_type = "..network_type
        end
        if network_size ~= nil then
            sql = sql .. ",network_size = "..network_size
        end
        if tags ~= nil then
            sql = sql .. ",tags = '"..tags.."'"
        end
        if desc ~= nil then
            sql = sql .. ",`desc` = '"..desc.."'"
        end
        if sql == "UPDATE base_info SET " then
            res:json({
                success = false,
                message = "没有传递需要更新的参数！"
            })
        else
            sql = sql .. "WHERE id = "..id
            sql = string.gsub(sql,"SET ,","SET ")
            local flag,result,sqlstate, err, errno = mysql:query(sql,true)

            local ids = req.body.ids
            -- 如果有依赖模块，则需要补充依赖数据到关系表ref_base
            if flag == true and ids ~= nil then
                local sql_del = "DELETE FROM ref_base WHERE p_id="..id
                mysql:query(sql_del,true)

                local sql_ref = "INSERT INTO ref_base(`p_id`, `c_id`) VALUES "
                local sql_ref_values = ""
                for i, v in ipairs(ids) do
                    sql_ref_values = sql_ref_values .. "("..id..", "..v.."),"
                end
                sql_ref_values = string.sub(sql_ref_values, 1, -2)
                sql_ref = sql_ref .. sql_ref_values
                mysql:query(sql_ref,true)
            end

            res:json({
                success = flag,
                data = result
            })
        end
    end
end)

return productRouter
